//
//  CinemaViewController.m
//  代号：47
//
//  Created by 杨佩璋 on 15/6/30.
//  Copyright (c) 2015年 杨佩璋. All rights reserved.
//

#import "CinemaViewController.h"
#import "SetupViewController.h"
#import "DTViewController.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapSearchKit/AMapSearchAPI.h>
#define APIKey @"c0f24200f0ef6af9359df2356b92c38b"
#import "RequestManager.h"
#import "CinemaModel.h"
#import "LORefresh.h"
#import "CinemaDataViewController.h"





@interface CinemaViewController ()<MAMapViewDelegate,UITableViewDataSource,UITableViewDelegate,RequestDelegate>

@property(nonatomic,retain)UITableView *tableView;

@property(nonatomic,assign)CGFloat str1;
@property(nonatomic,assign)CGFloat str2;

@property(nonatomic,retain)NSMutableArray *array;



@property(nonatomic,assign)BOOL bb;


@end
@implementation CinemaViewController
-(void)dealloc{

    _tableView = nil;
    _array = nil;
    [super dealloc];

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.bb = YES;
    // Do any additional setup after loading the view.
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"setup" style:(UIBarButtonItemStyleBordered) target:self action:@selector(handleSetupBtnAction:)];
    self.navigationItem.leftBarButtonItem = item;
    self.array = [NSMutableArray array];
    [self initDT];
    
}
#pragma ==============================================================================
-(void)initDT{
    [MAMapServices sharedServices].apiKey = APIKey;
    MAMapView *mapView = [[MAMapView alloc] init];
    mapView.delegate = self;
    mapView.showsUserLocation = YES;
}
-(void)mapView:(MAMapView *)mapView didUpdateUserLocation:(MAUserLocation *)userLocation updatingLocation:(BOOL)updatingLocation{
    self.str1 = userLocation.coordinate.latitude;
    self.str2 = userLocation.coordinate.longitude;
    if (_bb == YES) {
        [self requestData];
        [self createTableView];
        _bb = NO;
    }
}

#pragma =================================================================================
#pragma ============创建tableView=====================
-(void)createTableView{

    UIImageView *imageView1 = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kScreen_width, (kScreen_height - 64 - 49)/ 3)] autorelease];
    imageView1.image = [UIImage imageNamed:@"dianying_meitu_1.jpg"];
    [self.view addSubview:imageView1];
    
    self.automaticallyAdjustsScrollViewInsets = NO;//取消自动布局
    self.tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 64 + (kScreen_height - 64 - 49)/ 3, kScreen_width, (kScreen_height - 64 - 49)/ 3 *2) style:(UITableViewStylePlain)] autorelease];
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource =self;
    _tableView.delegate = self;
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView addRefreshWithRefreshViewType:LORefreshViewTypeHeaderGif refreshingBlock:^{
    [_array removeAllObjects];
    [self requestData];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _array.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if (_array.count > 0) {
        CinemaModel *model;
        model = _array[indexPath.row];
        cell.textLabel.text = model.title;
        
        

    }
    
    return cell;

}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    CinemaDataViewController *cinemaDataVC = [[CinemaDataViewController alloc] init];
    CinemaModel *model;
    model = _array[indexPath.row];
    cinemaDataVC.strCZ = model.ID;
    [self.navigationController pushViewController:cinemaDataVC animated:YES];
    
}


#pragma =====================网络请求=========================

-(void)requestData{


    NSString *str = [NSString stringWithFormat:@"http://m.api.tuan800.com/sub/movie20?cmd=GET_DEAL_CINEMAS&lat=%f&lng=%f&image=iosm1&page=1&size=15",_str1,_str2];
    
    RequestManager *request = [[RequestManager alloc] initWithRequestURL:str requestModel:GET parameter:nil];

    request.delegate = self;
    [request startRequest];

}

-(void)requestDidFinishWithData:(NSData *)data{


    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves) error:nil];
    for (NSDictionary *dict in array) {
        CinemaModel *model = [[CinemaModel alloc] init];
        [model setValuesForKeysWithDictionary:dict];
        [_array addObject:model];
        [model release];
    }
    [_tableView reloadData];
    [_tableView.gifHeader endRefreshing];
    
}
-(void)requestError:(NSError *)error{

    NSLog(@"%@",error);


}

- (void)handleSetupBtnAction:(UIButton *)btn{
    
    SetupViewController *setupVc = [[[SetupViewController alloc] init] autorelease];
//    [UIView transitionFromView:self.view toView:setupVc.view duration:0.5 options:(UIViewAnimationOptionTransitionCrossDissolve) completion:^(BOOL finished) {
//        [setupVc.setupView.buttons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//            AmazingButton *button = obj;
//            [button show];
//        }];
//    }];
    [self.navigationController.view addSubview:setupVc.view];
    [setupVc.setupView.buttons enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            AmazingButton *button = obj;
            [button show];
        }];
    


}




@end
